package cn.inyaa.admin.sys.dao.dsl.impl;

import cn.inyaa.admin.sys.bean.QInyaaSysRole;
import cn.inyaa.admin.sys.bean.QInyaaSysUser;
import cn.inyaa.admin.sys.dao.dsl.InyaaUserDslDao;
import cn.inyaa.admin.sys.vo.InyaaSysUserVo;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class InyaaUserDslDaoImpl implements InyaaUserDslDao {

    private final JPAQueryFactory jpaQueryFactory;

    @Override
    public List<InyaaSysUserVo> findUserList() {
        QInyaaSysUser qUser = QInyaaSysUser.inyaaSysUser;
        QInyaaSysRole qRole = QInyaaSysRole.inyaaSysRole;
        JPAQuery<InyaaSysUserVo> jpaQuery = jpaQueryFactory
                .select(Projections.bean(InyaaSysUserVo.class
                        , qUser.id, qUser.username, qUser.password,
                        qUser.name, qUser.email, qUser.loginDate,
                        qUser.loginIp, qUser.avatar, qUser.roleId, qRole.roleName)).from(qUser);
        jpaQuery.leftJoin(qRole).on(qUser.roleId.eq(qRole.id));
        jpaQuery.orderBy(qUser.createTime.desc());
        return jpaQuery.fetch();
    }
}
